package com.adrian.controller;

import com.adrian.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: Adrian
 * @Date: 2020/3/16 20:40
 * @Description:
 */

@RestController
@Scope("prototype")
public class ComuserController {

    @Resource
    private RestTemplate restTemplate;

    @Autowired
    DiscoveryClient client;

    @RequestMapping("/comuser")
    public List<User> listUser() {

        List<String> services = client.getServices();
        System.out.println("注册中心上服务列表有:" + services);
        List<ServiceInstance> instances = client.getInstances("springcloud-producer");
        for (ServiceInstance element : instances) {
            System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t" + element.getUri());
        }
        ServiceInstance instance = instances.get(0);
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/list";
        return this.restTemplate.getForObject(url, List.class);
    }
}
